<template>
	<jk-dialog
		v-model="showDialog"
		:title="`生产通知单`"
		width="1100px"
		:show-custom-btn="pageType !== 'detail'"
		custom-type="primary"
		custom-name="保存并下达"
		:custom-btn-loading="saveBtnLoading"
		:show-confirm-btn="false"
		:show-cancel-btn="pageType !== 'detail'"
		:show-close-btn="pageType === 'detail'"
		@on-close="onVisibleChange(false)"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-custom="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" :hide-required-asterisk="pageType === 'detail'" label-width="110px" :show-message="false">
			<div ref="box1">
				<div class="margin-bottom-4 flex-between-center">
					<div>
						<div class="flex-start-center" style="font-size: 13px;">
							<el-tag v-if="!ruleForm.code" type="primary" effect="plain" class="">
								<span class="el-icon-tickets item-gap"></span>
								<span>基本信息</span>
							</el-tag>
							<div v-else style="font-size: 16px;">
								<span class="el-icon-star-off item-gap"></span>
								<span>{{ ruleForm.code }}</span>
							</div>
							<el-tag v-show="ruleForm.id" size="mini" type="info" class="margin-left-10">
								<notice-state-bar :status="ruleForm.auditState"></notice-state-bar>
							</el-tag>
						</div>
					</div>
					<div class="flex-end-center">
						<machine-state-bar
							v-show="ruleForm.id"
							renovate-permission-prefix="production:prdNoticeMachineChange:complete"
							close-permission-prefix="production:prdNoticeMachineClose"
							open-permission-prefix="production:prdNoticeMachineOpening"
							:machine-task-list="machineTaskList"
							:form-data="ruleForm"
							@on-open="onOpenBtn"
							@on-close="onCloseBtn"
							@on-renovate="finishRenovate"
						></machine-state-bar>
						<technology-notice-bar
							v-show="ruleForm.id"
							:option-list="detailDataMachineList"
							@on-notice="onNoticeBtn"
						></technology-notice-bar>
					</div>
				</div>
				<jk-card>
					<el-row class="flex-start-center-wrap">
						<el-col :span="8">
							<el-form-item class="margin-bottom-10" label="单据日期">
								<span class="read-only-2">{{ ruleForm.belongDate }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item class="margin-bottom-10" label="品种">
								<span class="read-only-2">{{ ruleForm.productName }}</span>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item class="margin-bottom-10" label="批号">
								<span class="read-only-2">{{ ruleForm.batchCode }}</span>
							</el-form-item>
						</el-col>
						<!--<elspanol8:>
							<el-form-item class="margin-bottom-10" label="工序">
								<span class="read-only-2">{{ ruleForm.processName }}</span>
							</el-form-item>
						</el-col>-->
						<el-col :span="8">
							<el-form-item class="margin-bottom-0" label="生产类型">
								<span class="read-only-2">{{ ruleForm.ifTest ? '试产打样' : '正式生产' }}</span>
							</el-form-item>
						</el-col>
						<!--<elspanol8:>
							<el-form-item class="margin-bottom-10" label="区域">
								<span class="read-only-2">{{ ruleForm.workshopName }}</span>
							</el-form-item>
						</el-col>-->
						<el-col :span="8">
							<el-form-item class="margin-bottom-0" label="排产数量">
								<span v-if="pageType === 'detail'" class="read-only-2">{{ ruleForm.productionQty }}</span>
								<jk-number-input v-else v-model="ruleForm.productionQty" placeholder="排产数量"></jk-number-input>
							</el-form-item>
						</el-col>
						<!--<elspanol8:>
                            <el-form-item class="margin-bottom-10" label="定量(g/m)">
                                <div class="flex-start">
                                    <span v-if="pageType === 'detail' || ruleForm.auditState === 3" class="read-only-2">{{ ruleForm.gramWeight }}</span>
                                    <jk-number-input v-else v-model="ruleForm.gramWeight" style="width: 100%;" placeholder="克重" @change="onGramWeightChange($event, 'gramWeight')" />
                                    <span>~</span>
                                    <span v-if="pageType === 'detail' || ruleForm.auditState === 3" class="read-only-2">{{ ruleForm.meters }}</span>
                                    <jk-number-input v-else v-model="ruleForm.meters" style="width: 100%;" placeholder="米长" @change="onGramWeightChange($event, 'meters')" />
                                </div>
                            </el-form-item>
                        </el-col>-->
						<!--输入颜色-->
						<!--<el-col v-if="processCode.toUpperCase() !== 'span'"8:>
							<el-form-item class="margin-bottom-10" :label="getColorInfoByProcess(processCode).inputName">
								<span v-if="pageType === 'detail'" class="read-only-2">{{ ruleForm.inputTubeColorList ? ruleForm.inputTubeColorList.map(x => x.name).join(',') : '' }}</span>
								<checkboxPopover
									v-else
									:title="getColorInfoByProcess(processCode).inputName"
									:default-list.sync="ruleForm.inputTubeColorList"
									:option-list="inputTubeColorList"
									@change="onToggleColorChange($event, 'inputTubeColorList')"
								></checkboxPopover>
							</el-form-item>
						</el-col>-->
						<!--输出颜色-->
						<!--<el-col :span="8">
							<el-form-item class="margin-bottom-10" :label="getColorInfoByProcess(processCode).outName">
								<span v-if="pageType === 'detail'" class="read-only-2">{{ ruleForm.outputTubeColorList ? ruleForm.outputTubeColorList.map(x => x.name).join(',') : '' }}</span>
								<checkboxPopover
									v-else
									:title="getColorInfoByProcess(processCode).outName"
									:default-list.sync="ruleForm.outputTubeColorList"
									:option-list="outTubeColorList"
									@change="onToggleColorChange($event, 'outputTubeColorList')"
								></checkboxPopover>
							</el-form-item>
						</el-col>
						&lt;!&ndash;纸管颜色&ndash;&gt;
						<el-col v-if="processCode ? processCode.toUpperCase() === 'XS' : false" :span="8">
							<el-form-item class="margin-bottom-0" label="纸管颜色">
								<span v-if="pageType === 'detail'" class="read-only-2">{{ ruleForm.paperTubeList ? ruleForm.paperTubeList.map(x => x.name).join(',') : '' }}</span>
								<checkboxPopover
									v-else
									title="纸管颜色"
									:default-list.sync="ruleForm.paperTubeList"
									:option-list="ltColorList"
								></checkboxPopover>
							</el-form-item>
						</el-col>-->
						<el-col :span="8">
							<el-form-item class="margin-bottom-0" label="备注">
								<el-input v-model="ruleForm.remark" type="textarea" :rows="1"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</jk-card>
			</div>
			<div ref="box2" class="flex-between-center margin-bottom-4 margin-top-10">
				<div class="flex-start-center" style="align-items: flex-end">
					<el-button v-if="pageType !== 'detail'" class="item-gap" type="primary" size="mini" icon="el-icon-plus" @click="onAddMachineBtn">生产机台</el-button>
					<span style="font-size: 13px;color: #808695;">(共计: {{ ruleForm.prdNoticeMachineList ? ruleForm.prdNoticeMachineList.length : 0 }}台)</span>
				</div>
				<div class="flex-end-center">
					<!--<div><el-button type="default" :disabled="!ruleForm.id" icon="el-icon-printer" size="mini" class="item-gap" @click="onPrintMachineBtn">打印</el-button></div>-->
					<div class="flex-start-center">
						<span style="font-size: 13px;color: #515a6e;">机台状态:</span>
						<jk-state-bar v-for="(item, index) in openStateList" :key="item.id" class="margin-left-10" :state="item.id" :state-list="openStateList"></jk-state-bar>
					</div>
				</div>
			</div>
			<notice-machine-list
				:height="dialogTableHeight - 10"
				:process-code="processCode"
				:page-type="pageType"
				:input-color-list="inputTubeColorList"
				:show-input-color="false"
				:out-color-list="outTubeColorList"
				:zl-color-list="ltColorList"
				:machine-list.sync="ruleForm.prdNoticeMachineList"
			></notice-machine-list>
		</el-form>
		<select-machine-gantt-dialog
			:form-data="ruleForm"
			:checked-data="checkMachineData"
			:dialog-state="selectMachineGanttDialogState"
			@on-visible-change="onVisibleChangeOfSelectMachineGanttDialog"
			@on-confirm="onConfirmBtnOfSelectMachineGanttDialog"
		></select-machine-gantt-dialog>
		<open-dialog
			:operation-type="openOperationType"
			:select-machine-item="selectMachineItem"
			:dialog-state="openDialogState"
			@on-visible-change="onVisibleChangeOfOpenDialog"
			@on-confirm="onConfirmChangeOfOpenDialog"
		></open-dialog>
		<close-dialog
			:select-machine-item="selectMachineItem"
			:dialog-state="closeDialogState"
			@on-visible-change="onVisibleChangeOfCloseDialog"
			@on-confirm="onConfirmChangeOfCloseDialog"
		></close-dialog>
	</jk-dialog>
</template>

<script>
    import remoteSearchProductInput from '../../order/components/remote-search-product-input';
    import remoteSearchTechnologyOrderInput from '../../order/components/remote-search-technology-order-input';
    import {
        productionProcessPrdNoticeCreate,
        productionProcessPrdNoticeGet, productionProcessPrdNoticeUpdate, productionProcessPrdNoticeUpdateRelease,
        spinSpecSheetTaskBatchCreate
    } from '../../../../api/production/xsNotice';
    import { formatDate, tipsBar } from '../../../../utils/jk/jk-tool';
    import { mathJsDiv } from '../../../../utils/jk/jk-cals';
    import checkboxPopover from '../../order/components/form-box/checkbox-popover';
    import { spinSpecSheetGet } from '../../../../api/technology/order';
    import { openStateList } from '../../../../utils/jk/status-option-list';
    import { productionPrdOrderGet } from '../../../../api/production/order';
    import { productionPrdNoticeMachineChangeCompleteRenovation, productionPrdNoticeMachineChangePage } from '../../../../api/production/xsRenovateOrder';
    import noticeMachineList from '../../productionNoticeOrder/components/notice-machine-list';
    import noticeStateBar from '../../productionNoticeOrder/components/notice-state-bar';
    import machineStateBar from '../../productionNoticeOrder/components/machine-state-bar';
    import technologyNoticeBar from '../../productionNoticeOrder/components/technology-notice-bar';
    import remoteSearchOrderInput from '../../productionNoticeOrder/components/remote-search-order-input';
    import selectMachineGanttDialog from '../../productionNoticeOrder/components/select-machine-gantt-dialog';
    import initFormData from '../../productionNoticeOrder/components/form-box/init-form-data';
    import openCloseRenovate from '../../productOpenCloseMachine/components/open-close-renovate';
    import permissionMixin from '../../../../mixins/permission-mixin';
    import getTubeColor from '../../foreSpinPrd/foreSpinningPlan/components/form-box/get-tube-color';
    import handlerCache from '../../../../mixins/handler-cache';
    import calHeight from '../../../../mixins/cal-dialog-height';
    export default {
        components: {
            noticeMachineList,
            noticeStateBar,
            machineStateBar, technologyNoticeBar, remoteSearchOrderInput, checkboxPopover, selectMachineGanttDialog, remoteSearchProductInput, remoteSearchTechnologyOrderInput
        },
        mixins: [initFormData, openCloseRenovate, permissionMixin, getTubeColor, calHeight],
        props: {
            pageType: {
                type: String,
                default: ''
            },
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            },
            formData: {
                type: Object,
                default() {
                    return {};
                }
            }
        },
        data() {
            return {
                apiItem: {
                    'save': productionProcessPrdNoticeCreate,
                    'update': productionProcessPrdNoticeUpdate
                },
                processCode: 'XS',
                showDialog: false,
                checkMachineData: [],
                showContentLoading: false,
                openStateList: openStateList,
                selectMachineGanttDialogState: false,
                saveBtnLoading: false,
                workshopList: [],
                processList: [],
                machineTaskList: [],
                saveDialogState: false,
                detailDataMachineList: [],
                dateVal: ''
            };
        },
        methods: {
            onDailyYieldChange(e) {
                this.ruleForm.prdNoticeMachineList.forEach(x => {
                    if (!x.specSheet.code) {
                        if (x.openingState !== 4) {
                            x.specSheet.dailyYield = e;
                            x.dailyYield = e;
                        }
                    }
                });
            },
            onAddMachineBtn() {
                // 除了已了机，不允许重复添加
                this.checkMachineData = this.ruleForm.prdNoticeMachineList ? this.ruleForm.prdNoticeMachineList.filter(x => x.openingState !== 4) : [];
                this.selectMachineGanttDialogState = true;
            },
            onNoticeBtn(id) {
                spinSpecSheetTaskBatchCreate([id]).then(res => {
                    if (res.data.status === 200) {
                        tipsBar(this, 'custom', 'success', '通知成功!');
                        this.getDetailRequest();
                    }
                });
            },
            onConfirmBtnOfSelectMachineGanttDialog(arr) {
                this.insertMachineData(arr);
                this.selectMachineGanttDialogState = false;
            },
            insertMachineData(arr) {
                const technologyInfo = JSON.parse(JSON.stringify(this.technologyOrderInfo));
                // 没有工艺单时，默认定量和台日单产
                if (!this.ruleForm.specSheetId) {
                    technologyInfo.dailyYield = this.ruleForm.dailyYield;
                    technologyInfo.meters = this.ruleForm.meters;
                    technologyInfo.gramWeight = this.ruleForm.gramWeight;
                }
                // 机台的翻改日期、计划开台日期
                let changeD = '';
                if (formatDate(Date.now()) <= this.ruleForm.planStartDate) {
                    changeD = this.ruleForm.planStartDate;
                } else {
                    changeD = formatDate(Date.now());
                }
                arr.forEach(x => {
                    technologyInfo.gramWeight = this.ruleForm.productOrder.gramWeight;
                    technologyInfo.meters = this.ruleForm.productOrder.meters;
                    this.ruleForm.prdNoticeMachineList.push({
                        changeDate: changeD,
                        time: '10:00:00',
                        machineId: x.id,
                        machineCode: x.code,
                        machineName: x.name,
                        ltMachineId: x.ltMachineId,
                        ltMachineCode: x.ltMachineCode,
                        ltMachineName: x.ltMachineName,
                        machineModelName: x.machineModelName,
                        productionQty: null,
                        productionDays: null,
                        subPlanStartDate: changeD,
                        subPlanEndDate: '',
                        dailyYield: this.ruleForm.dailyYield,
                        processId: x.processId,
                        processCode: x.processCode,
                        processName: x.processName,
                        specSheet: technologyInfo,
                        openingState: 0,
                        inputTubeColorList: this.ruleForm.inputTubeColorList ? JSON.parse(JSON.stringify(this.ruleForm.inputTubeColorList)) : [],
                        outputTubeColorList: this.ruleForm.outputTubeColorList ? JSON.parse(JSON.stringify(this.ruleForm.outputTubeColorList)) : [],
                        paperTubeList: this.ruleForm.paperTubeList ? JSON.parse(JSON.stringify(this.ruleForm.paperTubeList)) : []
                    });
                });
            },
            onRemoveBtn(scope) {
                this.ruleForm.prdNoticeMachineList.splice(scope.rowIndex, 1);
            },
            getOrderDetailRequest() {
                if (this.ruleForm.orderIssued.order.id) {
                    return productionPrdOrderGet({ id: this.ruleForm.orderIssued.order.id }).then(res => {
                        if (res.data.status === 200) {
                            const arr = res.data.res.coarseSandList ? res.data.res.coarseSandList.map(x => {
                                delete x.id;
                                return x;
                            }) : [];
                            this.$set(this.ruleForm, 'spunYarnBomItemList', arr);
                        }
                    });
                }
            },
            onSelectTechnologyOrderChange(e) {
                this.clearFormItemVal('technology');
                if (e && Object.keys(e).length) {
                    this.$set(this.ruleForm, 'specSheetId', e.id);
                    this.$set(this.ruleForm, 'specSheetCode', e.code);
                    this.$set(this.ruleForm, 'machineModelId', e.machineModelId);
                    this.$set(this.ruleForm, 'machineModelCode', e.machineModelCode);
                    this.$set(this.ruleForm, 'machineModelName', e.machineModelName);
                    this.$set(this.ruleForm, 'dailyYield', e.dailyYield);
                    this.$set(this.ruleForm, 'moistureRegain', e.moistureRegain);
                    this.$set(this.ruleForm, 'efficiencyPercent', e.efficiencyPercent);
                    this.$set(this.ruleForm, 'gramWeight', e.gramWeight);
                    this.$set(this.ruleForm, 'meters', e.meters);
                    this.ruleForm.prdNoticeMachineList.forEach(x => {
                        this.$set(x, 'dailyYield', e.dailyYield);
                        this.$set(x, 'gramWeight', e.gramWeight);
                        this.$set(x, 'meters', e.meters);
                        this.$set(x, 'specSheetId', e.id);
                        this.$set(x, 'specSheetCode', e.code);
                        this.$set(x, 'specSheet', {}); // specSheetId和specSheet是二选一的关系,specSheet是针对没有工艺单时直接录入工艺项目
                        if (x.productionQty && x.dailyYield && x.subPlanStartDate) {
                            const days = Math.ceil(mathJsDiv(x.productionQty, e.dailyYield));
                            this.$set(x, 'productionDays', days);
                            this.$set(x, 'subPlanEndDate', formatDate(Date.parse(x.subPlanStartDate) + (days * 24 * 60 * 60 * 1000)));
                        }
                    });
                    this.getTechnologyOrderDetailRequest(e.id);
                } else {
                    this.$set(this.ruleForm, 'specSheetId', null);
                    this.$set(this.ruleForm, 'specSheetCode', '');
                    this.$set(this.ruleForm, 'machineModelId', null);
                    this.$set(this.ruleForm, 'machineModelCode', '');
                    this.$set(this.ruleForm, 'machineModelName', '');
                    this.$set(this.ruleForm, 'dailyYield', null);
                    this.$set(this.ruleForm, 'moistureRegain', null);
                    this.$set(this.ruleForm, 'efficiencyPercent', null);
                    this.$set(this.ruleForm, 'meters', null);
                    this.$set(this.ruleForm, 'gramWeight', null);
                }
            },
            getTechnologyOrderDetailRequest(id) {
                return spinSpecSheetGet({ id }).then(res => {
                    if (res.data.status === 200) {
                        this.technologyOrderInfo = res.data.res;
                        this.technologyOrderInfo.specSheetParamList.map(x => {
                            x.val = x.dataType === 1 ? Number(x.val) : x.val;
                            x.valMax = x.dataType === 1 ? Number(x.valMax) : x.valMax;
                            x.valMin = x.dataType === 1 ? Number(x.valMin) : x.valMin;
                            return x;
                        });
                    }
                });
            },
            onVisibleChangeOfSelectMachineGanttDialog(e) {
                this.selectMachineGanttDialogState = e;
            },
            onPrintBtn() {
                const routeUrl = this.$router.resolve({
                    name: 'printOrder',
                    query: {
                        id: this.ruleForm.productOrderId
                    }
                });
                window.open(routeUrl.href, '_blank');
            },
            onPrintMachineBtn() {
                const routeUrl = this.$router.resolve({
                    name: 'printMachine',
                    query: {
                        id: this.ruleForm.id
                    }
                });
                window.open(routeUrl.href, '_blank');
            },
            onJumpOrder() {
                this.$store.dispatch('tagsView/delView', { path: this.$route.path });
                this.$toOtherRouter({
                    name: 'editPrdOrder',
                    query: {
                        id: this.ruleForm.productOrder.id
                    }
                });
            },
            onJumpList() {
                this.$store.dispatch('tagsView/delView', { path: this.$route.path });
                this.$toMainRouter({
                    name: 'ProductionNoticeOrder'
                });
            },
            onJump(id) {
                this.$store.dispatch('tagsView/delView', { path: this.$route.path });
                this.$toOtherRouter({
                    name: 'editProductionNoticeOrder',
                    query: {
                        id: id
                    }
                });
            },
            onSelectEmpChange(e) {
                if (Object.keys(e).length) {
                    this.$set(this.ruleForm, 'plannerId', e.id);
                    this.$set(this.ruleForm, 'plannerName', e.name);
                }
            },
            onSelectCustomerChange(e) {
                if (Object.keys(e).length) {
                    this.$set(this.ruleForm, 'customerId', e.id);
                    this.$set(this.ruleForm, 'customerName', e.name);
                }
            },
            onSelectBatchCodeChange(e) {
                if (Object.keys(e).length) {
                    this.$set(this.ruleForm, 'batchCode', e.batchCode);
                }
            },
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                params.prdNoticeMachineList = this.ruleForm.prdNoticeMachineList.map(x => x);
                params.machineQty = params.prdNoticeMachineList.length;
                if (params.workshopId) params.workshopName = this.workshopList.find(x => x.id === params.workshopId).name;
                delete params.productOrderBatchItem;
                return params;
            },
            onConfirmBtn(type) {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        if (this.ruleForm.prdNoticeMachineList && this.ruleForm.prdNoticeMachineList.length) {
                            this.createAndUpdateRequest(type);
                        } else {
                            this.$tipsBar(this, 'custom', 'warning', '机台不能为空!');
                        }
                    } else {
                        this.$tipsBar(this, 'custom', 'warning', '细纱管、纸管颜色或其他必填项未填写完整!');
                    }
                });
            },
            createAndUpdateRequest(type) {
                this.saveBtnLoading = true;
                // 新增
                if (!this.dataId) {
                    this.ruleForm.id = null;
                    productionProcessPrdNoticeCreate(this.getParamData()).then(res => {
                        this.saveBtnLoading = false;
                        if (res.data.status === 200) {
                            this.$tipsBar(this, 'save');
                            this.$emit('on-confirm');
                            // this.onJump(res.data.res);
                        }
                    });
                    return;
                }
                let api = null;
                if (this.ruleForm.auditState) {
                    api = productionProcessPrdNoticeUpdate;
                } else {
                    api = productionProcessPrdNoticeUpdateRelease;
                }
                // 编辑
                api(this.getParamData()).then(res => {
                    this.saveBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$emit('on-confirm');
                        this.$tipsBar(this, 'save');
                        this.getDependentData();
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            showLoading(e) {
                this.showContentLoading = e;
            },
            getDetailRequest() {
                return productionProcessPrdNoticeGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        const resData = res.data.res ? JSON.parse(JSON.stringify(res.data.res)) : {};
                        resData.prdNoticeMachineList.forEach(k => {
                            if (k.specSheet && k.specSheet.specSheetParamList) {
                                k.specSheet.specSheetParamList.forEach(x => {
                                    // 做类型转换，避免组件报错
                                    x.val = x.dataType === 1 ? Number(x.val) : x.val;
                                    x.valMax = x.dataType === 1 ? Number(x.valMax) : x.valMax;
                                    x.valMin = x.dataType === 1 ? Number(x.valMin) : x.valMin;
                                });
                            }
                        });
                        resData.machineParams = resData.machineParams || {};
                        resData.orderIssued = resData.orderIssued || { order: {}};
                        resData.paperTubeList = resData.paperTubeList || [];
                        this.ruleForm = resData;
                        this.detailDataMachineList = res.data.res ? res.data.res.prdNoticeMachineList : [];
                        if (this.ruleForm.specSheetId) {
                            this.getTechnologyOrderDetailRequest(this.ruleForm.specSheetId);
                        }
                        // 通过后纺排产中心选择机台时，将机台放入对应的工序内
                        if (this.$route.query.machineList) {
                            let machineData = JSON.parse(this.$route.query.machineList).map(x => {
                                return {
                                    id: x.machineId,
                                    code: x.machineCode,
                                    name: x.machineName,
                                    processId: x.processId,
                                    processCode: x.processCode,
                                    processName: x.processName
                                };
                            });
                            // 过滤已有的且未了机的机台
                            machineData.forEach(newMachineItem => {
                                newMachineItem.isExit = false;
                                this.ruleForm.prdNoticeMachineList.forEach(exitMachineItem => {
                                    // 未了机的
                                    if (newMachineItem.id === exitMachineItem.machineId && exitMachineItem.openingState < 4) {
                                        newMachineItem.isExit = true;
                                    }
                                });
                            });
                            machineData = machineData.filter(x => !x.isExit);
                            this.insertMachineData(machineData);
                        }

                        this.$emit('return', res.data.res);
                    }
                });
            },
            getMachineTaskListRequest() {
                if (this.ruleForm.code && this.ruleForm.id) {
                    return productionPrdNoticeMachineChangePage({
                        prdNoticeCode: this.ruleForm.code
                    }).then(res => {
                        if (res.data.status === 200) {
                            this.machineTaskList = res.data.res;
                        }
                    });
                } else {
                    return new Promise((resolve, reject) => resolve());
                }
            },
            finishRenovate(id) {
                this.$confirm(`确定"完成翻改"吗？`, '提示', { type: 'warning' }).then(() => {
                    productionPrdNoticeMachineChangeCompleteRenovation({ id: id }).then(res => {
                        if (res.data.status === 200) {
                            this.getListRequest();
                        }
                    });
                }).catch(() => {});
            },
            getListRequest() {
                this.getDependentData();
            },
            onToggleColorChange(data, key) {
                this.ruleForm.prdNoticeMachineList.forEach(x => {
                    if (x.openingState !== 4) {
                        this.$set(x, key, data);
                    }
                });
            },
            onGramWeightChange(data, key) {
                this.ruleForm.prdNoticeMachineList.forEach(x => {
                    if (x.openingState !== 4) {
                        this.$set(x, key, data);
                        this.$set(x.specSheet, key, data);
                    }
                });
            },
            async getDependentData() {
                this.showLoading(true);
                // this.dataId = this.$route.query.id ? Number(this.$route.query.id) : null;
                if (this.pageType !== 'detail') {
                    this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                    if (!this.dataId) {
                        this.$set(this.ruleForm, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                        this.$set(this.ruleForm, 'workshopCode', this.$store.state.user.userWorkshop.defaultWorkshopCode);
                        this.$set(this.ruleForm, 'workshopName', this.$store.state.user.userWorkshop.defaultWorkshopName);
                        this.$set(this.ruleForm, 'belongDate', formatDate(Date.now()));
                    }
                }
                await this.getLtColorListRequest();
                if (this.dataId) await this.getDetailRequest();
                if (this.getColorInfoByProcess(this.processCode)) await this.getInputColorListRequest(this.getColorInfoByProcess(this.processCode).inputTypeCode);
                if (this.getColorInfoByProcess(this.processCode)) await this.getOutColorListRequest(this.getColorInfoByProcess(this.processCode).outTypeCode);
                await this.getMachineTaskListRequest();
                this.$nextTick(() => {
                    this.$refs.ruleForm.clearValidate();
                });
                this.showLoading(false);
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };

</script>
